# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=vsgImGui
pkgbase=mingw-w64-${_realname,,}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname,,}"
         "${MINGW_PACKAGE_PREFIX}-${_realname,,}-docs")
pkgver=0.7.0
_imguisha=993fa347495860ed44b83574254ef2a317d0c14f
_implotsha=f156599faefe316f7dd20fe6c783bf87c8bb6fd9
pkgrel=1
pkgdesc='Integration of VulkanSceneGraph with ImGui (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='http://www.vulkanscenegraph.org/'
msys2_repository_url='https://github.com/vsg-dev/vsgImGui'
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-vulkan-loader"
         "${MINGW_PACKAGE_PREFIX}-vulkanscenegraph")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-winpthreads-git"
             "${MINGW_PACKAGE_PREFIX}-vulkan-headers"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-graphviz")
source=("${msys2_repository_url}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "https://github.com/ocornut/imgui/archive/${_imguisha}/imgui-${_imguisha}.tar.gz"
        "https://github.com/epezent/implot/archive/${_implotsha}/implot-${_implotsha}.tar.gz")
sha256sums=('016191dfa4953fd381990166cf7df6f1328ced02411afcc7387e9121bf825d92'
            'a543367a7b45959d7ea21eeac808e26d3b2264ebc6d59b5046135791e97f4ca9'
            'd779ce33326343724d1e0fa23d4960cc9acaf4af584bc0134e504e7717ccc4bb')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  rm -rf "${srcdir}/${_realname}-${pkgver}/src/imgui"
  cp -r "${srcdir}/imgui-${_imguisha}" "${srcdir}/${_realname}-${pkgver}/src/imgui"

  rm -rf "${srcdir}/${_realname}-${pkgver}/src/implot"
  cp -r "${srcdir}/implot-${_implotsha}" "${srcdir}/${_realname}-${pkgver}/src/implot"
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_{SHARED,STATIC}_LIBS=ON \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build "build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build "build-${MSYSTEM}" --target docs
}

package_vsgimgui() {
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install "build-${MSYSTEM}"

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE.md" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname,,}/LICENSE.md"
}

package_vsgimgui-docs() {
  pkgdesc+=" (documentation)"
  depends=()

  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname,,}"
  cp -r "build-${MSYSTEM}/html" "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname,,}/"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
